package com.ts.api.module.protocol.controller;

import com.gitee.apanlh.annotation.viewresolver.RequestParamVo;
import com.gitee.apanlh.util.reflection.ClassConvertUtils;
import com.gitee.apanlh.util.reflection.CopyUtils;
import com.gitee.apanlh.util.valid.ValidParam;
import com.gitee.apanlh.web.model.vo.RequestVO;
import com.gitee.apanlh.web.model.vo.ResultVO;
import com.ts.api.common.constant.protocol.UserProtocolEnum;
import com.ts.api.module.channel.entity.cache.ChannelCacheUser;
import com.ts.api.module.channel.service.ChannelLoginService;
import com.ts.api.module.protocol.entity.bo.ProtocolTemplateBO;
import com.ts.api.module.protocol.entity.po.ProductProtocolPO;
import com.ts.api.module.protocol.entity.po.UserProtocolPO;
import com.ts.api.module.protocol.entity.qo.ProductProtocolQO;
import com.ts.api.module.protocol.entity.qo.UserProtocolQO;
import com.ts.api.module.protocol.entity.vo.ProductProtocolVO;
import com.ts.api.module.protocol.entity.vo.ProtocolResultVO;
import com.ts.api.module.protocol.service.ProductProtocolService;
import com.ts.api.module.protocol.service.ProtocolTemplateService;
import com.ts.api.module.protocol.service.UserProtocolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 *  协议相关内容
 *  非公共协议->CommonProtocolController
 *  @author Pan
 */
@RestController
@RequestMapping("/protocol")
public class ProtocolController {

    @Autowired
    private UserProtocolService userProtocolService;
    @Autowired
    private ProductProtocolService productProtocolService;
    @Autowired
    private ChannelLoginService channelLoginService;
    @Autowired
    private ProtocolTemplateService protocolTemplateService;

    /**
     *  根据token获取协议中可填充模板内容
     *
     *  @param  requestVO 请求VO实体
     *  @return ResultVO
     *  @throws Exception
     */
    @GetMapping("/get/user/template")
    public ResultVO<Object> getUserTemplate(@RequestParamVo RequestVO requestVO) {
        //  获取当前用户
        ChannelCacheUser loginUser = channelLoginService.getLoginUser();
        //  获取用户模板
        ProtocolTemplateBO userTemplate = protocolTemplateService.getUserTemplate(loginUser.getPhone());
        return ResultVO.suc(userTemplate);
    }

    /**
     *  根据主体id返回该主体所有协议列表
     *
     *  @param  requestVO 请求VO实体
     *  @return ResultVO
     *  @throws Exception
     */
    @PostMapping("/list/product")
    public ResultVO<Object> listProduct(@RequestParamVo RequestVO requestVO) {
        Integer productId = requestVO.get("productId", Integer.class);
        if (ValidParam.isNull(productId)) {
            return ResultVO.fail("非法空参数");
        }

        //  查询主体协议对象
        ProductProtocolQO productProtocolQO = new ProductProtocolQO();
        productProtocolQO.setProductId(Long.valueOf(productId));
        productProtocolQO.setHasNeedText(false);
        //  获取主体协议
        List<ProductProtocolPO> listProductProtocol = productProtocolService.list(productProtocolQO);

        //  返回主体协议
        List<ProductProtocolVO> listProductProtocolVO = CopyUtils.copyList(listProductProtocol, ProductProtocolVO.class);
        return ResultVO.suc(listProductProtocolVO);
    }

    /**
     *  根据主体协议id获取协议内容
     *
     *  @param  requestVO 请求VO实体
     *  @return ResultVO
     *  @throws Exception
     */
    @PostMapping("/get/product/text")
    public ResultVO<Object> getProductText(@RequestParamVo RequestVO requestVO) {
        Integer productProtocolId = requestVO.get("productProtocolId", Integer.class);
        if (ValidParam.isNull(productProtocolId)) {
            return ResultVO.fail("非法空参数");
        }

        //  获取主体协议内容
        ProductProtocolPO productProtocol = productProtocolService.get(Long.valueOf(productProtocolId));
        return ResultVO.suc(productProtocol);
    }

    /**
     *  返回用户已授权协议
     *
     *  @param  requestVO 请求VO实体
     *  @return ResultVO
     *  @throws Exception
     */
    @PostMapping("/get/user")
    public ResultVO<Object> getUserProtocol(@RequestParamVo RequestVO requestVO) {
        //  计划id
        Object pId = requestVO.get("pId");
        String phoneMd5 = String.valueOf(requestVO.get("phoneMd5"));

        if (ValidParam.isNull(pId, phoneMd5) || phoneMd5.length() < 32) {
            return ResultVO.fail("非法空参数");
        }

        //  查询协议对象
        UserProtocolQO userProtocolQO = new UserProtocolQO();
        userProtocolQO.setProductPlanId(ClassConvertUtils.toLong(pId.toString()));
        userProtocolQO.setPhoneMd5(phoneMd5);
        userProtocolQO.setType(UserProtocolEnum.PRODUCT.getType());

        List<UserProtocolPO> listUserProtocol = userProtocolService.list(userProtocolQO);
        if (ValidParam.isEmpty(listUserProtocol)) {
            return ResultVO.fail("非法参数");
        }

        //  添加协议
        ProtocolResultVO protocolResultVO = new ProtocolResultVO();
        listUserProtocol.forEach(userProtocolPO -> protocolResultVO.addProtocol(userProtocolPO.getText()));
        return ResultVO.suc(protocolResultVO);
    }
}
